import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../components/Login.vue'
import Home from '../components/Home.vue'
import Welcome from '../components/Welcome.vue'
import User from '../components/user/User.vue'


Vue.use(VueRouter)

const routes = [
    //路由重定向
    { path: '/', redirect: '/login' },
    { path: '/login', component: Login },
    {
        path: '/home', component: Home, redirect: '/Welcome',
        children: [
            { path: '/welcome', component: Welcome },
            { path: '/Users', component: User }
        ]
    },

]

const router = new VueRouter({
    routes
})
// 挂载路由导航守卫
router.beforeEach((to, from, next) => {
    // 如果访问的是登录页,直接放行
    if (to.path === '/login') return next();
    // 获取保存在本地token
    const tokenStr = window.sessionStorage.getItem('token');
    if (!tokenStr) return next('/login');
    next();

})

export default router
